home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_03 / allison / calc.c < prev    next >
C/C++ Source or Header  |  1995-01-03  |  3KB  |  136 lines

  1. LISTING 5 - A simple calculator that illustrates some <math.h>
  2. functions
  3.  
  4. /* calc.c:  Lame-brain Calculator -
  5.  *
  6.  *  For simplicity in parsing, this program
  7.  *  reads lines of the form:
  8.  *
  9.  *      value operation
  10.  *
  11.  *  where the value is optional in some cases.
  12.  *  For example, the following script computes
  13.  *  the integer part of sqrt(1 + 3.4*3.4):
  14.  *  
  15.  *      3.4 =
  16.  *      3.4 *
  17.  *      1 +
  18.  *      sqrt
  19.  *      floor
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <math.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <errno.h>
  28.  
  29. #define LINSIZ 40
  30.  
  31. char *getline(char *);
  32.  
  33. main()
  34. {
  35.     double  reg = 0.0;
  36.     char    line[LINSIZ];
  37.  
  38.     while (getline(line) != NULL)
  39.     {
  40.         char *op;
  41.         double val;
  42.  
  43.  
  44.         /* Parse command string */
  45.         val = strtod(line,&op);
  46.         while (isspace(*op))
  47.             ++op;
  48.         strupr(op);
  49.  
  50.         /* Perform operation */
  51.         errno = 0;
  52.         if (*op == '+')
  53.             reg += val;
  54.         else if (*op == '-')
  55.             reg -= val;
  56.         else if (*op == '*')
  57.             reg *= val;
  58.         else if (*op == '/')
  59.         {
  60.             if (val != 0)
  61.                 reg /= val;
  62.             else
  63.             {
  64.                 puts("ERROR>>> invalid divisor");
  65.                 continue;
  66.             }
  67.         }
  68.         else if (*op == '=')
  69.             reg = val;
  70.         else if (*op == '^')
  71.         {
  72.             if (val == 0.0)
  73.                 reg = 1.0;
  74.             else if (val == 0.5)
  75.                 reg = sqrt(reg);
  76.             else
  77.                 reg = pow(reg,val);
  78.         }
  79.         else if (strncmp(op,"NEGATE",1) == 0)
  80.             reg = -reg;
  81.         else if (strncmp(op,"MOD",1) == 0)
  82.         {
  83.             if (val == 0.0)
  84.             {
  85.                 puts("ERROR>>> invalid modulus");
  86.                 continue;
  87.             }
  88.             else
  89.                 reg = fmod(reg,val);
  90.         }
  91.         else if (strncmp(op,"CEIL",1) == 0)
  92.             reg = ceil(reg);
  93.         else if (strncmp(op,"FLOOR",1) == 0)
  94.             reg = floor(reg);
  95.         else if (strncmp(op,"ROUND",1) == 0)
  96.             reg = (reg < 0.0) ? ceil(reg - 0.5)
  97.                               : floor(reg + 0.5);
  98.         else if (strncmp(op,"SQRT",1) == 0)
  99.             reg = sqrt(reg);
  100.         else if (strncmp(op,"QUIT",1) == 0)
  101.             exit(0);
  102.         else if (*op != '\0')
  103.         {
  104.             puts("ERROR>>> invalid operation");
  105.             continue;
  106.         }
  107.  
  108.         if (errno)
  109.             perror("ERROR>>>");
  110.         else
  111.             printf("\t%s => %g\n",line,reg);
  112.     }
  113.     return 0;
  114. }
  115.  
  116. char *getline(char *buf)
  117. {
  118.     fputs("Calc> ",stdout);
  119.     fflush(stdout);
  120.     return gets(buf);
  121. }
  122.  
  123. /* Output:
  124. Calc> 3.4 =
  125.         3.4 = => 3.4
  126. Calc> 3.4 *
  127.         3.4 * => 11.56
  128. Calc> 1 +
  129.         1 + => 12.56
  130. Calc> sqrt
  131.         SQRT => 3.54401
  132. Calc> round
  133.         FLOOR => 3
  134. Calc> q
  135. */
  136.